<html>
<head>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/layer.js"></script>
</head>
    <body>
        <h1 id="msg">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</h1>
    </body>
    <script>
        var msg = document.getElementById("msg");
        var wsServer = 'ws://192.168.10.10:9502';
        //调用websocket对象建立连接：
        //参数：ws/wss(加密)：//ip:port （字符串）
        var ws = new WebSocket(wsServer);
        //onopen监听连接打开
        ws.onopen = function (evt) {
            //websocket.readyState 属性：
            /*
             CONNECTING    0    The connection is not yet open.
             OPEN    1    The connection is open and ready to communicate.
             CLOSING    2    The connection is in the process of closing.
             CLOSED    3    The connection is closed or couldn't be opened.
             */
            switch (ws.readyState) {
                case 0: layer.msg('通信服务未打开');break;
                case 1: layer.msg('连接成功');break;
                case 2: layer.msg('通信端口关闭，无法连接');break;
                case 3: layer.msg('通信服务已关闭');break;
            }
            {{--var rid = '{{$id}}';--}}
//            var data = '{"rid":"'+rid+'","role":"host"}';
//            ws.send(data);
            ws.send('{{$data}}');
        };

        //监听连接关闭
            ws.onclose = function (evt) {
                layer.msg('连接关闭');
            };

        //onmessage 监听服务器数据推送
        ws.onmessage = function (evt) {
            msg.innerHTML += evt.data +'<br>';
//            layer.msg('连接关闭');
        };
        //监听连接错误信息
            ws.onerror = function (evt, e) {
                layer.msg('连接错误: ' + evt.data);
            };

    </script>
</html>